package com.yueyang.sojourn.server.service.member;

import com.yueyang.sojourn.server.common.baymax.web.result.JAssert;
import com.yueyang.sojourn.server.model.po.MemberMerch;
import com.yueyang.sojourn.server.model.po.MemberMerchSnap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 会员商品快照
 */
@Service
public class MemberMerchSnapService {
    @Autowired
    private MemberMerchSnapMapper memberMerchSnapMapper;
    @Autowired
    private MemberMerchService memberMerchService;

    public MemberMerchSnap findById(Long id) {
        return this.memberMerchSnapMapper.selectByPrimaryKey(id);
    }

    public void insert(MemberMerchSnap snap) {
        this.memberMerchSnapMapper.insert(snap);
    }

    /**
     * 保存商品快照
     * @param memberMerchId
     * @return
     */
    public MemberMerchSnap snapshot(Integer memberMerchId, Long orderId) {
        MemberMerch memberMerch = memberMerchService.findById(memberMerchId);
        JAssert.pass(memberMerch != null, "会员商品不存在,id=" + memberMerchId);
        MemberMerchSnap snap = new MemberMerchSnap();
        BeanUtils.copyProperties(memberMerch, snap, "id");
        snap.setMemberMerchId(memberMerchId);
        snap.setSnapTime(new Date());
        snap.setOrderId(orderId);
        this.insert(snap);
        return snap;
    }
}